package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.Product;
import com.briup.jz.bean.ProductExample;
import com.briup.jz.bean.extend.ProductExtend;
import com.briup.jz.dao.ProductMapper;
import com.briup.jz.dao.extend.ProductExtendMapper;
import com.briup.jz.service.IProductService;
import com.briup.jz.utils.CustomerException;


@Service
public class ProductServiceImpl implements IProductService{
	
	@Autowired
	private ProductMapper productMapper;
	
	@Autowired
	private ProductExtendMapper productExtendMapper;
	
	//修改 增加
	@Override
	public void saveOrUpdate(Product product) throws CustomerException {
		if(product.getId()!=null) {
			productMapper.updateByPrimaryKey(product);
			
			
			} else {
				// 判断是否有同名的分类，如果有抛出异常
				
				ProductExample example = new ProductExample();
				example.createCriteria().andNameEqualTo(product.getName());
				List<Product> list = productMapper.selectByExample(example);
				if(list.size()>0) {
					throw new CustomerException("同名的分类已存在");
				}
				productMapper.insert(product);
			}
			}
	//查询
	@Override
	public List<Product> query(String name) {
		ProductExample example = new ProductExample();
		if(name!=null){
			example.createCriteria().andNameLike("%"+name+"%");
		}
		List<Product> list = productMapper.selectByExample(example);
		return list;
	}
		
		//删除		
	    @Override
		public void deleteById(long id) throws CustomerException {
			Product product = productMapper.selectByPrimaryKey(id);
			if(product == null) {
				throw new CustomerException("要删除的信息不存在");
			}
			productMapper.deleteByPrimaryKey(id);
			
		}
	    
	    @Override
		public List<ProductExtend> queryCascade(String name) {
			// TODO Auto-generated method stub
			return productExtendMapper.selectProduct(name);
		}
	

}
